#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void Delay_MSec(unsigned int msec)
{
    QTime _Timer = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < _Timer )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

int convert_yuv_to_rgb_pixel(int y, int u, int v)
{
        unsigned int pixel32 = 0;
        unsigned char *pixel = (unsigned char *)&pixel32;
        int r, g, b;
        r = y + (1.370705 * (v-128));
        g = y - (0.698001 * (v-128)) - (0.337633 * (u-128));
        b = y + (1.732446 * (u-128));
        if(r > 255) r = 255;
        if(g > 255) g = 255;
        if(b > 255) b = 255;
        if(r < 0) r = 0;
        if(g < 0) g = 0;
        if(b < 0) b = 0;
        pixel[0] = r ;
        pixel[1] = g ;
        pixel[2] = b ;
        return pixel32;
}

int convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
{
        unsigned int in, out = 0;
        unsigned int pixel_16;
        unsigned char pixel_24[3];
        unsigned int pixel32;
        int y0, u, y1, v;

        for(in = 0; in < width * height * 2; in += 4)
        {
                pixel_16 =
                                yuv[in + 3] << 24 |
                                yuv[in + 2] << 16 |
                                yuv[in + 1] <<  8 |
                                yuv[in + 0];
                y0 = (pixel_16 & 0x000000ff);
                u  = (pixel_16 & 0x0000ff00) >>  8;
                y1 = (pixel_16 & 0x00ff0000) >> 16;
                v  = (pixel_16 & 0xff000000) >> 24;
                pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
                pixel_24[0] = (pixel32 & 0x000000ff);
                pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
                pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
                rgb[out++] = pixel_24[0];
                rgb[out++] = pixel_24[1];
                rgb[out++] = pixel_24[2];
                pixel32 = convert_yuv_to_rgb_pixel(y1, u, v);
                pixel_24[0] = (pixel32 & 0x000000ff);
                pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
                pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
                rgb[out++] = pixel_24[0];
                rgb[out++] = pixel_24[1];
                rgb[out++] = pixel_24[2];
        }
        return 0;

}

void MainWindow::acceptConnection()
{
    QByteArray ba;
    uchar buff[640*480*3];
    while(qus->hasPendingDatagrams())
    {
        ba.resize(qus->pendingDatagramSize());
        qus->readDatagram(ba.data(), ba.size());
        switch (ba.data()[ba.size()-1]) {
        case 0x01:
            memcpy(yuvbuff,ba.data(),64999);
            break;
        case 0x02:
            memcpy(yuvbuff+64999,ba.data(),64999);
            break;
        case 0x03:
            memcpy(yuvbuff+64999*2,ba.data(),64999);
            break;
        case 0x04:
            memcpy(yuvbuff+64999*3,ba.data(),64999);
            break;
        case 0x05:
            memcpy(yuvbuff+64999*4,ba.data(),64999);
            break;
        case 0x06:
            memcpy(yuvbuff+64999*5,ba.data(),64999);
            break;
        case 0x07:
            memcpy(yuvbuff+64999*6,ba.data(),64999);
            break;
        case 0x08:
            memcpy(yuvbuff+64999*7,ba.data(),64999);
            break;
        case 0x09:
            memcpy(yuvbuff+64999*8,ba.data(),64999);
            break;
        case 0x00:
            memcpy(yuvbuff+64999*9,ba.data(),ba.size()-1);
            break;
        default:
            break;
        }

//        if(        ba.data()[ba.size()-1] !=0xff
//                || ba.data()[ba.size()-2] !=0x45
//                || ba.data()[ba.size()-3] !=0xaa )
//            continue;
        convert_yuv_to_rgb_buffer((uchar* )yuvbuff,buff,640,480);
        QImage *tempImage=new QImage((const uchar* )buff,640,480,QImage::Format_RGB888);
        QPixmap pixtemp = QPixmap::fromImage(*tempImage);
        ui->label->setPixmap(pixtemp);
       // ba.clear();
    }

}


void MainWindow::yuv420_toRGB(int width, int height, uchar *buf,uchar* RGBBuffer)
{
        uchar *YBuffer = buf;
        uchar *UBuffer = buf + height * width;
        uchar *VBuffer = buf + (int)(height*width*1.25);
        int Y, U, V, R, G, B;
        int writePos = 0;
        int i = 0, j = 0;
        int k = 0;
        for(i=0; i<height; i++)
        {
                for(j=0; j<width; j++)
                {
                        Y = YBuffer[j]-16;
                        k = (int)(j/2);
                        U = UBuffer[k]-128;
                        V = VBuffer[k]-128;
                        R = ((298*Y + 409*V + 128)>>8);
                        G = ((298*Y - 100*U - 208*V + 128)>>8);
                        B = ((298*Y + 516*U + 128)>>8);
                        RGBBuffer[writePos++] = (B<0) ? 0 : ((B>255) ? 255 : B);
                        RGBBuffer[writePos++] = (G<0) ? 0 : ((G>255) ? 255 : G);
                        RGBBuffer[writePos++] = (R<0) ? 0 : ((R>255) ? 255 : R);
                        RGBBuffer[writePos++] = 0xff;
                }

                YBuffer += width;
                if(i%2==1)
                {
                        UBuffer += width/2;
                        VBuffer += width/2;
                }
        }

}



void MainWindow::on_pushButton_clicked()
{
    qus->bind(QHostAddress::Any,21);
    ui->plainTextEdit->appendPlainText("开始监听");
    memset(yuvbuff,0,480*640*2);
    connect(qus, SIGNAL(readyRead()), this, SLOT(acceptConnection()));
}

void MainWindow::on_pushButton_2_clicked()
{
    qus->close();
}


